<?php
/**
 * Ajax数据格式化类
 * 
 * 用来生成Ajax需要的JSON数据。
 * 
 * @category   Framework
 * @package    Model
 * @author     Xinze <xinze@live.cn>
 * @copyright  Copyright (c) 2010, 黄新泽
 * @version    1.0
 * @todo       
 */
class Zero_Data
{
    /**
     * 控制器返回的数据,For ajax data format.
     * 
     * @access public
     * @var array
     */
    public $dataRows = array('head'=>array('id'=>0, 'error'=>false, 'msg'=>'', 'num'=>0),'body'=>'');

    public function __construct()
    {
        if (array_key_exists('id', $_REQUEST))
        {
            $this->dataRows['head']['id'] = trim($_REQUEST['id']);
        }
    }

    /*
    //废弃
    function buildXML($id, $data, $rows=1, $errorMessage='发生错误', $type='object', $operateType='object')
    {
        $outString=    
                    '<?xml version="1.0" encoding="UTF-8" ?>
                        <ajax-response>
                            <response type="'.$type.'" id="'.$id.'"     totalRows="'.$rows.'" errorMessage="'.$errorMessage.'"    operateType="'.$operateType.'">
                                '.$data.'
                            </response>
                        </ajax-response>';
        return $outString ;
    }

    //废弃
    function printXML($row=false)
    {
        header('Content-type: text/xml');
        
        if (!$row)
        {
            $row = $this->virtualRow;
        }

        $data    = convertArrayToProperties($row,'arytest',false,false);
        $outXML    = $this->buildXML($this->id, $data,$this->totalRows, $this->errorMessage);
        echo $outXML ;
    }

    //废弃
    function printHTML($row=false)
    {
        header('Content-type: text/xml');
        
        if (!$row)
        {
            $row = $this->virtualRow;
        }

        $outXML    = $this->buildXML($this->id, $row,$this->totalRows,    $this->errorMessage, 'object', 'element');

        echo $outXML ;
    }

    //废弃
    function buildXMLCDATA($id,    $data, $rows=1,    $errorMessage='发生错误', $type='object')
    {
        $outString=    
                    '<?xml version="1.0" encoding="UTF-8" ?>
                        <ajax-response>
                            <response type="'.$type.'" id="'.$id.'"     totalRows="'.$rows.'" errorMessage="'.$errorMessage.'">
                                <![CDATA['.$data.']]>
                            </response>
                        </ajax-response>';
        return $outString ;
    }

    //废弃
    function printXMLCDATA($row=false)
    {
        header('Content-type: text/xml');
        
        if (!$row)
        {
            $row = $this->virtualRow;
        }

        $data    = convertArrayToProperties($row,'arytest',false,false);
        $outXML    = $this->buildXMLCDATA($this->id, $data,$this->totalRows, $this->errorMessage);
        echo $outXML ;
    }
    */

    /**
     * 返回请求数据
     *
     * @return   string $json   返回数据
     * @access   public
     */
    function getJSON()
    {
        header('Content-type: text/html');

        return json_encode($this->getDataRows()) ;
    }

    /**
     * 返回请求数据
     *
     * @return   string $json   返回数据
     * @access   public
     */
    function getDataRows()
    {
        if (!$this->dataRows['head']['num'])
        {
            $this->dataRows['head']['num'] = count($this->dataRows['body']);
        }


        if (DEBUG)
        {
            if ($d = trim(ob_get_contents()))
            {
                $this->dataRows['debug'] = $d;
            }
            
        }

        ob_end_clean();
        ob_start();

        return $this->dataRows;
    }

    /**
     * 返回请求数据
     *
     * @return string   $json   返回数据
     * @access public
     */
    function printJSON()
    {
        echo $this->getJSON();
    }
}

?>